<template>
  <div class="upload-file" >
    <!-- <img wx :src="url" alt="" style="width:80px;height:60px"> -->
    <!-- <div v-if="url">
      <img width="100px" :src="url" alt="">
    </div> -->
    <div class="img-list" v-for="(item, index) of info" :key="index">
      <el-image
        :preview-src-list="[item]"
        :ref="`img${index}`"
        :src="item"
      ></el-image>
        <div class="maskBox">
            <div class="mask"></div>
            <div class="maskBtn">
                <i class="el-icon-zoom-in" @click.stop="handlePreview(info, index)"></i>
                <i class="el-icon-delete" @click.stop="handleRemove(info, index)"
                ></i>
            </div>
        </div>
    </div>
    <el-upload
      ref="fileUpload"
      action=""
      list-type="picture-card"
      :on-preview="handlePictureCardPreview"
      :on-remove="handleRemove"
      :show-file-list="false"
      :http-request="fnUploadRequest"
      :on-success="handleSuccess"
      :on-error="handleError"
      :before-upload="handleUpload"
      
      class="avatar-uploader"
    >
     <!-- <i class="el-icon-plus avatar-uploader-icon"></i> -->
    <el-tooltip class="item" effect="dark" content="文件格式仅支持jpg、jpeg、gif、mp4、mp3" placement="bottom-start">
      <el-button size="small" type="primary">点击上传</el-button>
    </el-tooltip>
      <!-- <slot></slot> -->
    </el-upload>
  </div>
</template>

<script>
import { uploadOSS } from '@/tools/oss';

export default {
  name: "index",
  props: {
      config: {
          type: Object,
          default: {}
      },
      number : {
        type: Number,
        default: 8,
      },
      desc: {
        type: String,
        default: '文件格式仅支持jpg、jpeg、gif、png'
      },
      imgae: {
        type: Array,
        default: ()=>{
          return []
        }
      }
  },
  data() {
    return {
      url: '',
      dialogVisible: false,
      info: this.imgae
    };
  },
  computed: {
  },
  methods: {
    handlePictureCardPreview(file) {
            this.dialogImageUrl = file.url
            this.dialogVisible = true
        },
    handleExceed(file, fileList){
      this.$message.error('上传失败，限制上传数量10个文件以内！');
    },
    handleUpload(file, fileList){
      var testmsg = file.name.substring(file.name.lastIndexOf('.') + 1)
      const extension = testmsg === 'mp4' || testmsg === 'avi' || testmsg === 'mov' || testmsg === 'mpg'
        || testmsg === 'jpg'|| testmsg === 'jpeg'|| testmsg === 'png'|| testmsg === 'gif'|| testmsg === 'mp3'

      const isLimit10M = file.size / 1024 / 1024 < 100
      var bool = false;
      if(extension && isLimit10M){
        bool = true;
      } else {
        bool = false;
      }
      if(!extension) {
        this.$message.error('请选择附件文件类型！');
        return bool;
      }
      if(!isLimit10M) {
        this.$message.error('上传失败，不能超过100M！');
        return bool;
      }
      return bool;
    },
    handleSuccess(res) {
      // console.log(res);
      if (res) {
        // this.$emit('fileData', res)
        this.$message.success("上传附件成功！");
      }
    },
    handleError(err){
      this.$message.error('上传附件失败！');
    },
    // 上传图片
    async fnUploadRequest(options) {
      try {
        // console.log(options);
        let file = options.file; // 拿到 file
        let res = await uploadOSS(file, this.config)
        if(this.number >1){
          this.info.push(res.url)
        }else{
          this.info = [res.url]
        }
        // 返回数据
        let data = this.info
        this.$emit("fileData", data);
        this.$message.success("上传附件成功！");
      } catch (e) {
        this.$message.error('上传附件失败！');
      }
    },
   // 预览图片
    handlePreview(e, index) {
        this.$refs[`img${index}`][0].showViewer = true
    },
    // 删除
    handleRemove(type, index) {
        let that=this
        this.info.splice(index, 1)
        that.$emit('fileData',this.info);
    },
  },
};
</script>

<style  lang="scss" scoped>
::v-deep .el-upload,
.el-upload--picture-card {
  // width: 50px;
  height: 0px;
  border: none;
  line-height: 0;
  display: block;
  background: #f5f6fb;
}
.el-upload {
  width: 50px;
}
.img-cont {
  width: 50px;
  height: 24px;
  background: #f5f6fb;
  .img-icon {
    color: #ccc;
  }
  .img-text {
    font-size: 12px;
    height: 24px;
    color: #000;
  }
}
.upload-file {
    display: flex;
    flex-wrap: wrap;
    margin-bottom: 30px;

    .avatar-uploader {
        margin-left: 10px !important;
        .avatar {
            width: 152px;
            height: 152px;
            display: block;
            padding: 5px;
            border-radius: 4px;
            border: 1px dashed #d9d9d9;
        }
    }
    .el-upload--picture-card {
        width: 152px;
        height: 152px;
    }
    .img-list:hover .maskBox {
        display: block;
    }
    .avatar-uploader .el-upload {
        cursor: pointer;
        position: relative;
        overflow: hidden;
    }
    .avatar-uploader .el-upload:hover {
        border-color: #409eff;
    }
    .avatar-uploader-icon {
        font-size: 28px;
        color: #8c939d;
        width: 152px;
        height: 152px;
        line-height: 152px;
        border: 1px dashed #d9d9d9;

        span {
            font-size: 12px;
            color: rgb(161, 163, 167);
            position: absolute;
            bottom: -25px;
            left: 27px;
        }
    }
    .svg-icon {
        // position: absolute;
        border: 1px dashed #d9d9d9;
    }
    .progress-box {
        border: 1px dashed #d9d9d9;
        height: 152px;
        width: 152px;
        position: absolute;
        z-index: 5;
        top: 0;
        left: 0;
        background: #ffffff;
        .el-progress-circle {
            padding: 5px;
            height: 152px !important;
            width: 152px !important;
            padding: 0;
        }
    }

    > div {
        margin-top: 10px;
    }
    .img-list {
        box-sizing: border-box;
        flex: 1;
        max-width: 152px;
        min-width: 152px;
        height: 152px;
        margin: 10px 10px 0 10px;
        position: relative;
        padding: 5px;
        border: 1px dashed #c2c6d1;
        display: flex;
        align-items: center;
        justify-content: center;
        overflow: hidden;
        img {
            width: 100%;
            height: 100%;
        }

        .avatar {
            width: 60px;
            height: 60px;
        }
        .svg-icon {
            border: 0;
        }
    }
    .maskBox {
        display: none;
        position: absolute;
        z-index: 5;
        width: 152px;
        height: 152px;
        top: 0;
        left: 0;

        .mask {
            width: 152px;
            height: 152px;
            position: absolute;
            background-color: #191919;
            opacity: 0.4;
            top: -1px;
            left: -1px;
            border: solid 1px #ebebeb;
            border-radius: 3px;
            transition: 0.2s;
        }
        .maskBtn {
            box-sizing: border-box;
            position: absolute;
            width: 152px;
            height: 152px;
            padding-top: 60px;
            text-align: center;

            i {
                color: #ffffff;
                font-size: 25px;
                margin: 0 5px;
                cursor: pointer;
            }
        }
    }
    .fileName {
        position: absolute;
        bottom: -30px;
        left: 0;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
        width: 100px;
    }
}
</style>